#include "registerwindow.h"
#include <QHBoxLayout>
RegisterWindow::RegisterWindow(QWidget *parent)
    : QDialog(parent)
    , lineEditUsername(new QLineEdit(this))
    , lineEditPassword(new QLineEdit(this))
    , lineEditConfirmPassword(new QLineEdit(this))
    , buttonRegister(new QPushButton("注册",this))
    , labelTitle(new QLabel("注册界面",this))
    , strengthLabel(new QLabel(this))
{
    setupUI();
}

RegisterWindow::~RegisterWindow(){}

void RegisterWindow::setupUI(){
    setWindowTitle("注册界面");
    resize(400,300);

    labelTitle->setAlignment(Qt::AlignHCenter);
    labelTitle->setStyleSheet("font-size:24px; font-weight:bold;");

    lineEditUsername->setPlaceholderText("请输入用户名!");
    lineEditPassword->setPlaceholderText("请输入密码!");
    lineEditPassword->setEchoMode(QLineEdit::Password);
    lineEditConfirmPassword->setPlaceholderText("确认密码!");
    lineEditConfirmPassword->setEchoMode(QLineEdit::Password);

    buttonRegister->setStyleSheet("QPushButton { font-size: 16px; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; }"
                               "QPushButton:hover { background-color: #45a049; }");

    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    QHBoxLayout *passwordLayout=new QHBoxLayout();
    strengthLabel->setFixedSize(100, 20);
    passwordLayout->addWidget(lineEditPassword);
    passwordLayout->addWidget(strengthLabel);
    lineEditConfirmPassword->setFixedWidth(275);
    mainLayout->addWidget(labelTitle);
    mainLayout->addSpacing(20);
    mainLayout->addWidget(lineEditUsername);
    mainLayout->addLayout(passwordLayout);
    mainLayout->addWidget(lineEditConfirmPassword);
    mainLayout->addWidget(buttonRegister);
    mainLayout->setSpacing(0);

    setLayout(mainLayout);
    connect(buttonRegister,&QPushButton::clicked,this,&RegisterWindow::handleRegister);

    connect(lineEditPassword,&QLineEdit::textChanged,this,[=](const QString &text){
        bool hasUppercase=text.contains(QRegExp("[A-Z]"));
        bool hasLowercase=text.contains(QRegExp("[a-z]"));
        bool hasDigit=text.contains(QRegExp("\\d"));

        int strengthLevel= hasUppercase+hasLowercase+ hasDigit;
        if(strengthLevel==1){
            strengthLabel->setText("密码强度：弱");
            strengthLabel->setStyleSheet("color:red");
        }else if(strengthLevel==2){
            strengthLabel->setText("密码强度：中");
            strengthLabel->setStyleSheet("color:orange");

        }else if(strengthLevel==3){
            strengthLabel->setText("密码强度：强");
            strengthLabel->setStyleSheet("color:green");
        }
        else{
            strengthLabel->setText("密码强度：弱");
            strengthLabel->setStyleSheet("color:red");
        }
    });
}

bool RegisterWindow::validateInput() {
    QString username = lineEditUsername->text();
    QString password = lineEditPassword->text();
    QString confirmPassword = lineEditConfirmPassword->text();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::critical(this, "注册失败", "用户名和密码不能为空!");
        return false;
    }

    if (!username.contains(QRegExp("^[a-zA-Z0-9_]{3,16}$"))){
        QMessageBox::warning(this,"注册失败","用户名必须是3-16位的字母，数字 或下划线!");
        return false;
    }

    if(password.length()< 6){
        QMessageBox::warning(this,"注册失败","密码长度需大于6个字符!");
        return false;
    }

    if(!password.contains(QRegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$"))){
        QMessageBox::warning(this,"注册失败","密码需包括大写字母，小写字母及数字！");
        return false;
    }

    if (password != confirmPassword) {
        QMessageBox::critical(this, "注册失败", "两次输入的密码不一致!");
        return false;
    }


    QSettings settings("MyApp", "LoginDemo");
    if (settings.contains("user/" + username + "/password")) { // 检查密码键是否存在
        QMessageBox::warning(this, "注册失败", "用户名已存在！");
        return false;
    }

    return true;
}

void RegisterWindow::handleRegister() {
    if (!validateInput()) {
        return;
    }

    QString username = lineEditUsername->text();
    QString password = lineEditPassword->text();

    // 将存储逻辑封装
    QSettings settings("MyApp", "LoginDemo");
    QString groupPath = QString("user/%1").arg(username);

    settings.beginGroup(groupPath);
    settings.setValue("password", password);
    settings.setValue("remember", false);
    settings.endGroup();

    QMessageBox::information(this, "注册成功", "注册成功，请返回登录！");
    close();
}

